package com.luhuiguo.cms.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.inject.Inject;
import com.luhuiguo.cms.client.AppConstants;
import com.luhuiguo.cms.client.AppMessages;
import com.luhuiguo.cms.shared.event.AddArticleEvent;
import com.luhuiguo.cms.shared.event.AddArticleEventHandler;
import com.luhuiguo.cms.shared.event.AddChannelEvent;
import com.luhuiguo.cms.shared.event.AddChannelEventHandler;
import com.luhuiguo.cms.shared.event.ArticleCreatedEvent;
import com.luhuiguo.cms.shared.event.ArticleCreatedEventHandler;
import com.luhuiguo.cms.shared.event.ArticleUpdatedEvent;
import com.luhuiguo.cms.shared.event.ArticleUpdatedEventHandler;
import com.luhuiguo.cms.shared.event.EditArticleCancelledEvent;
import com.luhuiguo.cms.shared.event.EditArticleCancelledEventHandler;
import com.luhuiguo.cms.shared.event.EditArticleEvent;
import com.luhuiguo.cms.shared.event.EditArticleEventHandler;
import com.luhuiguo.cms.shared.event.EditChannelEvent;
import com.luhuiguo.cms.shared.event.EditChannelEventHandler;
import com.luhuiguo.cms.shared.event.FlashEvent;
import com.luhuiguo.cms.shared.event.LoadArticlesEvent;
import com.luhuiguo.cms.shared.event.LoadArticlesEventHandler;
import com.luhuiguo.cms.shared.model.ArticleModel;
import com.luhuiguo.cms.shared.model.ChannelModel;
import com.luhuiguo.cms.shared.model.UserModel;
import com.luhuiguo.gwt.dispatcher.client.DispatcherAsync;
import com.luhuiguo.gwt.presenter.client.EventBus;
import com.luhuiguo.gwt.presenter.client.widget.WidgetContainerDisplay;
import com.luhuiguo.gwt.presenter.client.widget.WidgetContainerPresenter;

public class MainPresenter extends WidgetContainerPresenter<MainPresenter.Display> {

    public interface Display extends WidgetContainerDisplay {

    }

    @SuppressWarnings("unused")
    private UserModel user;

    @SuppressWarnings("unused")
    private AppConstants constants;

    private AppMessages messages;

    @SuppressWarnings("unused")
    private DispatcherAsync dispatcher;

    private NavigationPresenter navigationPresenter;

    private EditChannelPresenter editChannelPresenter;

    private ArticlesPresenter articlesPresenter;

    private EditArticlePresenter editArticlePresenter;

    @Inject
    public MainPresenter(Display display, EventBus eventBus, DispatcherAsync dispatcher, AppConstants constants,
            AppMessages messages, NavigationPresenter navigationPresenter, EditChannelPresenter editChannelPresenter,
            ArticlesPresenter articlesPresenter, EditArticlePresenter editArticlePresenter) {
        super(display, eventBus, editChannelPresenter, articlesPresenter, editArticlePresenter);
        this.dispatcher = dispatcher;
        this.constants = constants;
        this.messages = messages;
        this.navigationPresenter = navigationPresenter;
        this.editChannelPresenter = editChannelPresenter;
        this.articlesPresenter = articlesPresenter;
        this.editArticlePresenter = editArticlePresenter;
        GWT.log("MainPresenter created");
    }

    @Override
    protected void onBind() {
        GWT.log("MainPresenter onBind");
        super.onBind();

        registerHandler(eventBus.addHandler(AddChannelEvent.TYPE, new AddChannelEventHandler() {
            @Override
            public void onAddChannel(AddChannelEvent event) {
                showAddChannel(event.getTreeItem());

            }

        }));

        registerHandler(eventBus.addHandler(EditChannelEvent.TYPE, new EditChannelEventHandler() {
            @Override
            public void onEditChannel(EditChannelEvent event) {
                showEditChannel(event.getTreeItem());
            }

        }));

        registerHandler(eventBus.addHandler(LoadArticlesEvent.TYPE, new LoadArticlesEventHandler() {

            @Override
            public void onLoadArticles(LoadArticlesEvent event) {
                showArticleList(event.getChannel());
            }

        }));

        registerHandler(eventBus.addHandler(AddArticleEvent.TYPE, new AddArticleEventHandler() {

            @Override
            public void onAddArticle(AddArticleEvent event) {
                showAddArticle(event.getChannel());

            }

        }));

        registerHandler(eventBus.addHandler(EditArticleEvent.TYPE, new EditArticleEventHandler() {

            @Override
            public void onEditArticle(EditArticleEvent event) {
                showEditArticle(event.getArticle());

            }

        }));

        registerHandler(eventBus.addHandler(EditArticleCancelledEvent.TYPE, new EditArticleCancelledEventHandler() {

            @Override
            public void onEditArticleCancelled(EditArticleCancelledEvent event) {
                showArticleList(event.getChannel());

            }

        }));

        registerHandler(eventBus.addHandler(ArticleCreatedEvent.TYPE, new ArticleCreatedEventHandler() {
            @Override
            public void onArticleCreated(ArticleCreatedEvent event) {
                showArticleList(event.getArticle().getChannel());
                eventBus.fireEvent(new FlashEvent(messages.articleCreated(event.getArticle().getTitle()), 2000));

            }
        }));

        registerHandler(eventBus.addHandler(ArticleUpdatedEvent.TYPE, new ArticleUpdatedEventHandler() {
            @Override
            public void onArticleUpdated(ArticleUpdatedEvent event) {
                showArticleList(event.getArticle().getChannel());
                eventBus.fireEvent(new FlashEvent(messages.articleUpdated(event.getArticle().getTitle()), 2000));

            }
        }));

    }

    private void showAddArticle(Long channel) {

        ArticleModel article = new ArticleModel();

        article.setChannel(channel);

        editArticlePresenter.revealDisplay(article);
    }

    private void showEditArticle(ArticleModel article) {

        editArticlePresenter.revealDisplay(article);
    }

    private void showAddChannel(TreeItem treeItem) {
        ChannelModel p = (ChannelModel) treeItem.getUserObject();
        ChannelModel channel = new ChannelModel();
        channel.setParent(p.getId());
        editChannelPresenter.revealDisplay(treeItem, channel);
    }

    private void showEditChannel(TreeItem treeItem) {
        ChannelModel channel = (ChannelModel) treeItem.getUserObject();
        editChannelPresenter.revealDisplay(treeItem, channel);
    }

    private void showArticleList(Long channel) {
        articlesPresenter.revealDisplay(channel);
    }

    public void revealDisplay(UserModel user) {
        this.user = user;
        // loadTreeItems();
        revealDisplay();
    }

}
